﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KaroInterface;

namespace Emerald
{
    public class AI : Player, IArtificialIntelligence
    {

        public AI(EColor c)
        {
            Color = c;
        }

        public AI()
        {

        }

        public IAction CalculateBestMove(GameState g)
        {
            return MiniMax.MiniMaxDecision(g,Color);
        }

        protected IAction CalculateRandomAdd(GameState g)
        {
            Action a = new Action();

            do
            {
                Random r = new Random();
                byte x = (byte) r.Next(8, 13);
                byte y = (byte) r.Next(8, 12);
                GameState gamestate = (GameState) g;
                if (gamestate.Board.BoardArray[x, y].Piece == null)
                    a = new Action(new BoardPoint(x, y), Color);
            } 
            while (!a.Set);

            return a;
        }

        public virtual string DoTurn(string gameStateString)
        {
            GameState g = new GameState(gameStateString);

            IAction a;

            if(g.CountPieces(Color.ToString())<6)
            {
                a = CalculateRandomAdd(g);
#if DEBUG
                Console.WriteLine("Adding a piece: "+a.ToString());
#endif

            }
            else
            {
                a = CalculateBestMove(g);
#if DEBUG
                Console.WriteLine("Moving a piece: "+a.ToString());
#endif
            }

            return a.DataString;
        }


    }
}
